package link;

public class leetCode24_swapPairs {

    public static void main(String[] args) {
        leetCode24_swapPairs a = new leetCode24_swapPairs();
        ListNode b = new ListNode(1);
        b.next = new ListNode(2);
        b.next.next = new ListNode(3);
        b.next.next.next = new ListNode(4);
        a.swapPairs(b);
    }


    public ListNode swapPairs(ListNode head) {
        if(head == null) return null;
        ListNode result = new ListNode(-1);
        result.next = head;
        ListNode tmp = result;
        while (tmp.next != null && tmp.next.next != null) {
            ListNode next = tmp.next;
            ListNode next2 = tmp.next.next;
            next.next = next2.next;
            next2.next = next;
            tmp.next = next2;
            tmp = next;

        }
        return result.next;
    }



    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

}
